/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn;

import cn.webrx.mapper.DbMapper;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;

/**
 * <p>Project: mybatis2021 - Demo
 * <p>Powered by webrx On 2021-10-22 17:29:11
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Demo {
    public final static Logger logger = LoggerFactory.getLogger(Demo.class);

    public static void main(String[] args) throws SQLException {
        //建立数据源 HikariDataSource
        var ds = new HikariDataSource();
        ds.setJdbcUrl("jdbc:mysql:/wxdb");
        ds.setUsername("root");

        //建立事务对象
        var tx = new JdbcTransactionFactory();
        //建立环境对象
        var env = new Environment("development", tx, ds);
        //建立配置对象相当于mybatis-config.xml文件
        var cfg = new Configuration(env);
        cfg.addMappers("cn.webrx.mapper");


        var sf = new SqlSessionFactoryBuilder().build(cfg);
        var ss = sf.openSession(true);

        var dbdao = ss.getMapper(DbMapper.class);
        logger.info(dbdao.ver());
    }
}
